import time
from selenium import webdriver
import string
import zipfile
import os

proxyHost = "tps131.kdlapi.com"
proxyPort = "15818"

# 代理隧道验证信息
proxyUser = "t18779592125934"
proxyPass = "ocxzglaw"

def create_proxy_auth_extension(proxy_host, proxy_port,
                                proxy_username, proxy_password,
                                scheme='http', plugin_path=None):
    if plugin_path is None:
        plugin_path = os.getcwd() + r'/{}_{}@http-dyn.abuyun.com_9020.zip'.format(proxy_username, proxy_password)

    manifest_json = """
       {
           "version": "1.0.0",
           "manifest_version": 2,
           "name": "Abuyun Proxy",
           "permissions": [
               "proxy",
               "tabs",
               "unlimitedStorage",
               "storage",
               "<all_urls>",
               "webRequest",
               "webRequestBlocking"
           ],
           "background": {
               "scripts": ["background.js"]
           },
           "minimum_chrome_version":"22.0.0"
       }
       """
    # print("sadadaasda")

    background_js = string.Template(
        """
          var config = {
              mode: "fixed_servers",
              rules: {
                  singleProxy: {
                      scheme: "${scheme}",
                      host: "${host}",
                      port: parseInt(${port})
                  },
                  bypassList: ["foobar.com"]
              }
            };
          chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
          function callbackFn(details) {
              return {
                  authCredentials: {
                      username: "${username}",
                      password: "${password}"
                  }
              };
          }
          chrome.webRequest.onAuthRequired.addListener(
              callbackFn,
              {urls: ["<all_urls>"]},
              ['blocking']
          );
          """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )

    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)
    # print("bbbbbb")

    return plugin_path


# 创建插件代理,如果文件已经创建,就不必再创建了
proxy_auth_plugin_path = create_proxy_auth_extension(
    proxy_host=proxyHost,
    proxy_port=proxyPort,
    proxy_username=proxyUser,
    proxy_password=proxyPass)
print("cccccccc")

def web_dri():

    option = webdriver.ChromeOptions()
    # 不显示UI界面,会报错
    option.add_argument('-headless')

    option.add_argument("--start-maximized")
    option.add_extension(proxy_auth_plugin_path)
    option.add_extension(r'D:\code\刷票\t18779592125934_ocxzglaw@http-dyn.abuyun.com_9020.zip')
    # print("ddddddd")r
    driver = webdriver.Chrome(options=option, executable_path=r'D:\code\刷票\chromedriver.exe')
    url = "http://vote.e23.cn/zhuanti/jnncp2020shibao/show_1.html?openid=&openidMd5=&subscribe=undefined&group=1"
    driver.get(url)
    driver.execute_script('window.scrollTo(0,1000)')   #滑到1000的位置
    driver.find_element_by_xpath('//*[@id="searchContent"]').send_keys('128')
    driver.find_element_by_id('searchBtn').click()
    time.sleep(1)
    driver.find_element_by_xpath('//*[@value="128"]').click()
    driver.find_element_by_xpath('//*[@id="form1"]/div[4]/div/div[2]').click()
    time.sleep(1)
    # current_windows = driver.window_handles
    try:
       driver.find_element_by_xpath('//*[@id="layui-layer1"]/div[3]/a').click()
       time.sleep(1)
    except Exception as e:
       print(e)
    driver.quit()


if __name__ == '__main__':
    while True:
        web_dri()